package com.ts.enterprise.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.enterprise.common.query.PageInfo;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2021/10/18 14:21
 * @Description
 */
public class PageModelUtils {

    @Data
    public static class PageMapModel<T> {
        List<T> rows;
        Long total;
        Long current;
        Long size;
        Long pages;

        public PageMapModel(Page page, List<T> list) {
            this.setRows(list);
            this.setTotal(page.getTotal());
            this.setCurrent(page.getCurrent());
            this.setSize(page.getSize());
            this.setPages(page.getPages());
        }

        public PageMapModel(IPage page, List<T> list) {
            this.setRows(list);
            this.setTotal(page.getTotal());
            this.setCurrent(page.getCurrent());
            this.setSize(page.getSize());
            this.setPages(page.getPages());
        }

        public PageMapModel(com.github.pagehelper.Page page, Long current, List<T> list) {
            this.setRows(list);
            this.setTotal(page.getTotal());
            this.setCurrent(current);
            this.setSize(Long.valueOf(page.getPageSize()));
            this.setPages(Long.valueOf(page.getPages()));
        }
    }

    public static Map<String, Object> getPageMap(Page page, Object list) {
        HashMap<String, Object> map = new HashMap<>(8);
        map.put("rows", list == null ? new ArrayList<>() : list);
        map.put("total", page.getTotal());
        map.put("current", page.getCurrent());
        map.put("size", page.getSize());
        map.put("pages", page.getPages());
        return map;
    }

    public static Map<String, Object> getPageMap(Page page, Object list, Map<String, Object> map) {
        map.put("rows", list == null ? new ArrayList<>() : list);
        map.put("total", page.getTotal());
        map.put("current", page.getCurrent());
        map.put("size", page.getSize());
        map.put("pages", page.getPages());
        return map;
    }

    public static Map<String, Object> getPageMap(IPage pageInfo, Object list) {
        HashMap<String, Object> map = new HashMap<>(8);
        return getStringObjectMap(pageInfo, list, map);
    }

    private static Map<String, Object> getStringObjectMap(PageInfo pageInfo, Object list, Map<String, Object> map) {
        map.put("rows", list == null ? new ArrayList<>() : list);
        map.put("total", pageInfo.getTotal());
        map.put("current", pageInfo.getCurrent());
        map.put("size", pageInfo.getSize());
        map.put("pages", pageInfo.getPages());
        return map;
    }

    private static Map<String, Object> getStringObjectMap(IPage pageInfo, Object list, Map<String, Object> map) {
        map.put("rows", list == null ? new ArrayList<>() : list);
        map.put("total", pageInfo.getTotal());
        map.put("current", pageInfo.getCurrent());
        map.put("size", pageInfo.getSize());
        map.put("pages", pageInfo.getPages());
        return map;
    }
}
